package com.boronstream.istar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;

import com.boronstream.istar.S.IActivityStarSymbol;
import com.boronstream.istar.http.PlayNetworkCenter;
import com.boronstream.istar.http.PlayRequestListener;
import com.boronstream.istar.http.PlayResponse;
import com.boronstream.istar.http.request.StarSymbolRequest;
import com.boronstream.istar.http.response.StarSymbolResponse;
import com.boronstream.istar.model.Symbol;
import com.boronstream.istar.utils.CommonDefine;
import com.boronstream.istar.utils.CommonDefine.IntentField;
import com.boronstream.istar.utils.CommonDefine.TimeExtra;
import com.boronstream.istar.utils.HelperUtils;
import com.boronstream.istar.utils.Logger;
import com.umeng.analytics.MobclickAgent;

public class StarSymbolActivity extends Activity implements PlayRequestListener {

	private static final String TAG = null;
	private IActivityStarSymbol self;
	private String starName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);
		self = new IActivityStarSymbol(this);
		self.mImgMemu.setVisibility(View.GONE);
		starName = getIntent().getStringExtra(IntentField.STAR_NAME);
		self.mTitle.setText(starName);
		initThreme();
//		self.mIconBack.setVisibility(View.VISIBLE);
		self.mImgFlipper.setVisibility(View.GONE);
		self.mIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		self.mImgShare.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Message msg=new Message();
				msg.what=MSG_SHARE_CONTENT;
				handler.sendMessageDelayed(msg, TimeExtra.ONE_SECOND/2);
			}
		});
		this.refreshAstroImage();
		this.startGetSymbol();
	}

	private void refreshAstroImage() {
		self.mIcon.setImageResource(R.drawable.icon_back);
		self.mIcon.setBackgroundResource(R.drawable.selector_title_text_bg);
		if (starName != null) {
			// TODO:
//			String[] astros = getResources().getStringArray(R.array.star_list);
//			for (int i = 0; i < astros.length; i++) {
//				if (starName.equals(astros[i])) {
//					String name = "star_" + i;
//					self.mIcon
//							.setImageResource(HelperUtils.getInstance()
//									.getDrawableIdByName(
//											StarSymbolActivity.this, name));
//					break;
//				}
//			}
		}
	}

	private final int MSG_SHARE_CONTENT=100;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what==MSG_SHARE_CONTENT) {
				shot();
				share();
			}
		};
	};
	private void shot() {
		try {
			View view = getWindow().getDecorView();
			Display display = this.getWindowManager().getDefaultDisplay();
			view.layout(0, 0, display.getWidth(), display.getHeight());
			view.setDrawingCacheEnabled(true);// 允许当前窗口保存缓存信息，这样getDrawingCache()方法才会返回一个Bitmap
			Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
			new File(CommonDefine.APP_SDCARD_FOLDER).mkdirs();
			File file = new File(CommonDefine.APP_SDCARD_FOLDER + "/share.png");
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			OutputStream outStream = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
			outStream.flush();
			outStream.close();
		} catch (Exception e) {
			Logger.e(TAG, "exception:" + e.getMessage());
			e.printStackTrace();
		}
	}

	private void share() {
		MobclickAgent.onEvent(this, "share","symbol");
		MobclickAgent.onEvent(this, "share", "symbol"+starName);
		Intent intent = new Intent(Intent.ACTION_SEND);
		File file = new File(CommonDefine.APP_SDCARD_FOLDER + "/share.png");
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
		intent.setType("image/*");
		intent.putExtra(Intent.EXTRA_SUBJECT, starName + "个性标签");
		intent.putExtra(Intent.EXTRA_TEXT, starName + "个性标签");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(Intent.createChooser(intent, getTitle()));

	}
	
private void initThreme(){
		
		SharedPreferences sPreferences=getSharedPreferences(
				getString(R.string.app_name), MODE_PRIVATE);
		int currentBg = sPreferences.getInt("default_bg", R.drawable.bg_repeat);
		self.mRootLayout.setBackgroundResource(currentBg);
		}

	private void startGetSymbol() {
		self.mProgressBar.setVisibility(View.VISIBLE);
		self.mMainContent.setVisibility(View.GONE);
		StarSymbolRequest request = new StarSymbolRequest();
		request.setStarName(starName);
		PlayNetworkCenter.getInstance().startRequest(request, this);
	}

	private void refresView(Symbol symbol) {
		if (symbol != null) {
			try {
				self.mSTitle.setText(starName + "的个性标签");
				self.mTvBoronTime.setText(symbol.getBoron_time().trim());
				self.mTvLover.setText(symbol.getLover().trim());
				self.mTvLabelLover.setText(starName + "的情人：");
				self.mTvSymbol.setText(symbol.getSymbol().trim());
				self.mTvQuardian.setText(symbol.getQuardian().trim());
				self.mTvRepresent.setText(symbol.getRepresent().trim());
				self.mTvProperty.setText(symbol.getProperty().trim());
				self.mTvCharacters.setText(symbol.getCharacters().trim());
				self.mTvLabelCharacters.setText(starName + "的个性：");
				self.mTvNoData.setVisibility(View.GONE);
			} catch (Exception e) {
			}
		}else {
			self.mTvNoData.setVisibility(View.VISIBLE);
			self.mMainContent.setVisibility(View.GONE);
			self.mProgressBar.setVisibility(View.GONE);
			self.mTvNoData.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					self.mTvNoData.setVisibility(View.GONE);
					startGetSymbol();
				}
			});
		}
	}

	@Override
	public void onRequestFinished(ResultType resultType, PlayResponse response) {
		self.mProgressBar.setVisibility(View.GONE);
		self.mMainContent.setVisibility(View.VISIBLE);
		StarSymbolResponse the=null;
		if (resultType == ResultType.SUCCESS && response != null
				&& response instanceof StarSymbolResponse) {
			the = (StarSymbolResponse) response;
			
		} else {
			self.mMainContent.setVisibility(View.GONE);
		}
		this.refresView(the==null?null:the.getSymbol());
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
